package com.zhang.test.common.util;

import com.zhang.test.common.base.ResponseCode;
import io.jsonwebtoken.*;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;

public class JjwtTokenUtil {
    private static String priKey = "wertyuiohvkasdfhkjh7h6789948hncxmcxmmxjsuwuubfsfgldfsgk";

    public static class JjwtTokenUtilHolder {
        private final static JjwtTokenUtil instance = new JjwtTokenUtil();
    }

    public synchronized static JjwtTokenUtil getInstance() {
        return JjwtTokenUtilHolder.instance;
    }


    /**
     * 获取 Token
     *
     * @param uid 用户id
     * @param exp 过期时间 ，单位小时
     * @return
     */
    public String getToken(String uid, int exp) {
        long expTime = System.currentTimeMillis() + 1000 * 60 * 60 * exp;
        return Jwts.builder().setSubject(uid)
                .setExpiration(new Date(expTime))
                .signWith(SignatureAlgorithm.HS256, priKey).compact();
    }

    /**
     * 获取 Token
     * @param uid
     * @return
     */
    public String getToken(String uid) {
        long expTime = System.currentTimeMillis() + 1000 * 60 * 60 * 24;
        return Jwts.builder().setSubject(uid)
                .setExpiration(new Date(expTime))
                .signWith(SignatureAlgorithm.HS256, priKey).compact();
    }

    /**
     * 检查 token 是否合法
     * @param token
     * @return
     */
    public JWTResult checkToken(String token) {
        try {
            Claims claims = Jwts.parser().setSigningKey(priKey)
                    .parseClaimsJws(token).getBody();
            String sub = claims.get("sub", String.class);
            return new JWTResult(true, sub, "合法请求", ResponseCode.SUCCESS_CODE.getCode());
        } catch (ExpiredJwtException e) {
            return new JWTResult(false, null, "token expired", ResponseCode.TOKEN_TIMEOUT_CODE.getCode());
        } catch (SignatureException e) {
            return new JWTResult(false, null, "invalid request", ResponseCode.NO_AUTH_CODE.getCode());
        } catch (Exception e) {
            return new JWTResult(false, null, "invalid request", ResponseCode.NO_AUTH_CODE.getCode());
        }

    }

    public static class JWTResult {
        private boolean status;
        private String uid;
        private String msg;
        private int code;

        public JWTResult() {
            super();
        }

        public JWTResult(boolean status, String uid, String msg, int code) {
            this.status = status;
            this.uid = uid;
            this.msg = msg;
            this.code = code;
        }


        public boolean isStatus() {
            return status;
        }

        public void setStatus(boolean status) {
            this.status = status;
        }

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}
